package com.yuyou.tas.admin.controller;

import com.yuyou.tas.util.bean.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yuyou.tas.admin.service.IAuthRoleService;
import com.yuyou.tas.admin.vo.req.ReqAuthRoleAdd;
import com.yuyou.tas.admin.vo.req.ReqAuthRoleUpdate;
import com.yuyou.tas.service.api.admin.entity.AuthRole;
import com.yuyou.tas.util.enums.admin.AuthRoleEnums;

import javax.servlet.http.HttpServletRequest;

@Api(value="AuthResourceController",description="角色管理接口")
@Validated
@RestController
@RequestMapping(value="/api/authRole")
public class AuthRoleController extends BaseController{

    @Autowired
    private IAuthRoleService authRoleService;


    @ApiOperation(value="查找角色列表")
    @RequestMapping(value= "/findAuthRoleList", method = { RequestMethod.GET ,RequestMethod.POST })
    public ReturnResult<List<AuthRole>> findAuthRoleList(HttpServletRequest request){
        LoginRedisObj loginObj = getUserObj(request);
        Boolean isSystem = loginObj.getIsSystem();
        Long organizationId = loginObj.getOrganizationId();
        List<AuthRole> authRoleList = authRoleService.findAuthRoleList(isSystem, organizationId);
        return ReturnResult.success(authRoleList);
    }

    @ApiOperation(value="角色列表（创建机构用）")
    @RequestMapping(value="/authRoleListForOrgan", method = { RequestMethod.GET ,RequestMethod.POST })
    public ReturnResult<List<AuthRole>> authRoleListForOrgan(HttpServletRequest request){
        LoginRedisObj loginObj = getUserObj(request);
        Boolean isSystem = loginObj.getIsSystem();
        List<AuthRole> authRoleList = authRoleService.findAuthRoleListForOrgan();
        return ReturnResult.success(authRoleList);
    }



    @ApiOperation(value="查找角色列表(分页)")
    @RequestMapping(value="/findAuthRolePage", method = { RequestMethod.GET ,RequestMethod.POST })
    public ReturnResult<AuthRole> findAuthRolePage(@Validated @ModelAttribute PageParams pageParams,
                                                   @RequestParam(value = "roleName", required = false) String roleName){
        Map<String, Object> conditions = new HashMap<>();
        Pagination<AuthRole> queryPgn = new Pagination<>();
        if(!StringUtils.isEmpty(roleName)){
            conditions.put("roleName", roleName.trim());
        }

        queryPgn.setConditions(conditions);
        queryPgn.setPageParams(pageParams);
        int total = authRoleService.findAuthRolePageCount(queryPgn);
        queryPgn.setTotal(total);
        queryPgn.toBePage();
        if(total > 0){
            queryPgn.setTotal(total);
            List<AuthRole> list = authRoleService.findAuthRolePageList(queryPgn);
            queryPgn.setList(list);
        }
        return ReturnResult.success(queryPgn);
    }



    @ApiOperation(value="添加角色")
    @RequestMapping(value="/addAuthRole", method = { RequestMethod.GET ,RequestMethod.POST })
    public ReturnResult<ResBoolSimpleInfo> addAuthRole(@Validated @ModelAttribute ReqAuthRoleAdd reqAuthRoleAdd){

        ResBoolSimpleInfo res = authRoleService.insertAuthRole(reqAuthRoleAdd);
        return ReturnResult.returnRes(res.isSuccess(), res.getMessage(),res.getData());
    }


    @ApiOperation(value="更新角色")
    @RequestMapping(value="/updateAuthRole", method = { RequestMethod.GET ,RequestMethod.POST })
    public ReturnResult<ResBoolSimpleInfo> updateAuthUser(@Validated @ModelAttribute ReqAuthRoleUpdate reqAuthRoleUpdate){


        ResBoolSimpleInfo res = authRoleService.updateAuthRole(reqAuthRoleUpdate);
        return ReturnResult.returnRes(res.isSuccess(), res.getMessage(), res.getData());
    }


    @ApiOperation(value="删除角色")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "角色id", paramType="query", required = true)})
    @RequestMapping(value="/deleteAuthRole", method= { RequestMethod.GET ,RequestMethod.POST })
    public ReturnResult<Void> deleteAuthRole(@NotBlank(message="id不能为空")String id){
    	AuthRole authRole=authRoleService.selectByPrimaryKey(id);
    	
    	if(authRole.getRoleType()!=null && authRole.getRoleType()==AuthRoleEnums.roleType.system.getValue()){
    		return ReturnResult.fail("系统字段不能删除");
    	}
    	
        authRoleService.deleteAuthRoleById(id);
        return ReturnResult.success("Ok");
    }


}
